#!/bin/bash
# Runs a textfile collector.

script_dir=$(dirname $0)
source ${script_dir}/ENV
file_path=$1
metric=`basename "$1"`
shift
script="${script_dir}/${file_path}"
prom_file="/opt/fonsview/3RD/node_exporter/textfile_collector/$metric".prom

# 判断脚本是否存在
if [[ ! -x "${script}" || -d "${script}" ]]; then
  # echo "ERROR: Can't find script for '$metric'. Aborting."
  exit 1
fi

# 判断执行结果是否存在
VALUE=$("${script}" "$@")
if [[ ! -n $VALUE ]]; then
    exit 1
fi

# 创建prom后缀文件
echo "${VALUE}" > "$prom_file".$$ && mv "$prom_file".$$ "$prom_file"
